from django.db import models
from django.utils import timezone


class Administrator(models.Model):
    username = models.CharField(max_length=50)
    password = models.CharField(max_length=50)

    def __str__(self):
        return self.username


class Good(models.Model):
    RAW_MATERIAL = 0
    PRODUCT = 1

    name = models.CharField(max_length=100, default='')
    stock = models.IntegerField(default=0)
    color = models.CharField(max_length=20, default='#ffffff')
    spec = models.CharField(max_length=50, default='')
    remarks = models.CharField(max_length=1000, default='')

    class Meta:
        abstract = True

    def __str__(self):
        return self.name


class Statistics(models.Model):
    name = models.CharField(max_length=100, default='')
    stock = models.IntegerField(default=0)

    def __str__(self):
        return self.name


class RawMaterial(Good):
    ...


class Product(Good):
    ...


class MetaRecord(models.Model):
    IN = 0
    OUT = 1
    NEW = 2
    DELETE = 3
    OPERA_CHOICE = [
        (IN, '入库'),
        (OUT, '出库'),
        (NEW, '新增'),
        (NEW, '删除'),
    ]

    opera_date = models.DateTimeField(name='date operated', default=timezone.now)
    opera_type = models.IntegerField(choices=OPERA_CHOICE, default=IN)
    operator = models.CharField(max_length=100, default='')
    count = models.IntegerField(default=0)
    info = models.CharField(max_length=500, default='')

    class Meta:
        abstract = True

    def __str__(self):
        return str.format('%s %d %s' % (self.opera_date, self.opera_type, self.operator))


class CommonRecord(MetaRecord):
    good_name = models.CharField(max_length=100, default='')

    def __str__(self):
        return str.format('%s %d %s' % (self.opera_date, self.opera_type, self.operator))


class ProductRecord(MetaRecord):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)


class RawMaterialRecord(MetaRecord):
    raw_material = models.ForeignKey(RawMaterial, on_delete=models.CASCADE)
